package fr.utt.lo02.CrazyEights.view;


import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

import fr.utt.lo02.CrazyEights.model.Game;
import fr.utt.lo02.CrazyEights.model.cards.CardRank;
import fr.utt.lo02.CrazyEights.model.cards.CardSuit;


/**
 * Represents a dialog to choose the suit
 * @author Jeremy
 *
 */
public class ChooseSuitDialog extends JDialog{
	private static final long serialVersionUID = 1L;
	private JButton club;
	private JButton diamond;
	private JButton heart;
	private JButton spade;
	
	private MainWindow window;
	
	/**
	 * Create the dialog
	 * @param window the parent window
	 */
	public ChooseSuitDialog(MainWindow window){
		super(window,"Choose Suit",true);
		this.window = window;
		this.setSize(550, 270);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		club = new JButton("Club");
		diamond = new JButton("Diamond");
		heart = new JButton("Heart");
		spade = new JButton("Spade");
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		
		JLabel title = new JLabel("Choose suit");
		title.setFont(new Font("Serif", Font.BOLD, 48));
		
		c.anchor = GridBagConstraints.CENTER;
		c.gridy = 0;
		this.add(title,c);
		c.gridy = 1;
		this.add(club,c);
		c.gridy = 2;
		this.add(diamond,c);
		c.gridy = 3;
		this.add(heart,c);
		c.gridy = 4;
		this.add(spade,c);
		
		club.addActionListener(new ChangeColorState());
		diamond.addActionListener(new ChangeColorState());
		heart.addActionListener(new ChangeColorState());
		spade.addActionListener(new ChangeColorState());
		
		
	}
	
	/**
	 * ActionListener for buttons to change suit
	 * @author Jeremy
	 *
	 */
	class ChangeColorState implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource()==club){
				Game.getInstance().getDiscardPile().setGameSuit(CardSuit.CLUBS);
			}else if(e.getSource()==diamond){
				Game.getInstance().getDiscardPile().setGameSuit(CardSuit.DIAMONDS);
			}else if(e.getSource()==heart){
				Game.getInstance().getDiscardPile().setGameSuit(CardSuit.HEARTS);
			}else if(e.getSource()==spade){
				Game.getInstance().getDiscardPile().setGameSuit(CardSuit.SPADES);
			}
			
			if(Game.getInstance().getDiscardPile().getCardOnTop().getRank()==CardRank.Joker){
				Game.getInstance().getPlayersList().skipNextPlayer();
			}
			else{
				Game.getInstance().getPlayersList().nextPlayer();
			}
			window.repaintGame();
			hideChooseSuitDialog();
		}
	}
	
	/**
	 * Show the dialog
	 */
	public void showChooseSuitDialog(){
		this.setVisible(true);
	}
	
	/**
	 * Hide the dialog
	 */
	public void hideChooseSuitDialog(){
		this.setVisible(false);
	}
}
